package cn.lycode.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaIgnore;
import cn.lycode.common.core.controller.BaseController;
import cn.lycode.common.core.domain.AjaxResult;
import cn.lycode.common.core.domain.entity.SysMenu;
import cn.lycode.common.core.domain.model.ConditionDTO;
import cn.lycode.common.core.domain.model.SelectIdsDTO;
import cn.lycode.system.domain.dto.sysmenu.MenuPermissionDTO;
import cn.lycode.system.domain.vo.MenuTreeVO;
import cn.lycode.system.domain.vo.SysMenuVO;
import cn.lycode.system.service.SysMenuService;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 菜单信息
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/sys-menu")
public class SysMenuController extends BaseController {

    private final SysMenuService menuService;

    //查询上级菜单
    @SaIgnore
    @GetMapping("/tree")
    public AjaxResult<List<MenuTreeVO>> queryParentListTree() {
        return AjaxResult.success(menuService.getSimpleMenuTree());
    }

    //查询菜单按钮权限是否存在
    @SaIgnore
    @PostMapping("/btn/exists")
    public AjaxResult<Boolean> findBtnPermission(@RequestBody MenuPermissionDTO dto) {
        return AjaxResult.success(menuService.hasExistsPerms(dto));
    }


    /**
     * **********************************增删改查****************************************************
     * 查询菜单列表
     */
    @ApiOperation(value = "获取全部菜单列表,条件查询")
    @SaCheckPermission("system:menu:list")
    @GetMapping("/list")
    public AjaxResult<List<SysMenuVO>> list() {
        List<SysMenuVO> sysMenus = menuService.selectMenuList(new ConditionDTO());
        return AjaxResult.success(sysMenus);
    }

    /**
     * 根据菜单编号获取详细信息
     */
    @ApiOperation(value = "根据菜单编号获取详细信息")
    @SaCheckPermission("system:menu:info")
    @GetMapping(value = "/{id}")
    public AjaxResult<SysMenu> getInfo(@PathVariable Long id) {
        SysMenu sysMenu = menuService.selectMenuById(id);
        return AjaxResult.success(sysMenu);
    }

    /**
     * 修改菜单
     */
    @ApiOperation(value = "修改菜单")
    @SaCheckPermission("system:menu:update")
    @PostMapping("/update")
    public AjaxResult<Object> updateMenu(@Validated @RequestBody SysMenu menu) {
        return toAjax(menuService.updateMenu(menu));
    }

    /**
     * 添加菜单
     */
    @ApiOperation(value = "添加菜单")
    @SaCheckPermission("system:menu:add")
    @PostMapping("/add")
    public AjaxResult<Object> addMenu(@Validated @RequestBody SysMenu menu) {
        return toAjax(menuService.addMenu(menu));
    }

    /**
     * 删除菜单
     */
    @ApiOperation(value = "删除菜单")
    @SaCheckPermission("system:menu:remove")
    @DeleteMapping("/remove")
    public AjaxResult<Object> delMenu(@RequestBody SelectIdsDTO dto) {
        menuService.remove(dto);
        return AjaxResult.success();
    }



}